package com.hiss.basic.starter.mybatis.utils;

import cn.hutool.core.util.StrUtil;
import com.hiss.basic.starter.core.utils.ServletUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class SortUtils {

    public static final String SORT_FIELD = "sort";

    private static final String ASC = "asc";

    private static final String DESC = "desc";

    /**
     * 获取排序规则，默认升序 asc
     *
     * @return true 升序，false 降序
     */
    public static boolean getSortRule() {
        try {
            HttpServletRequest request = ServletUtils.getRequest();
            String sortRule = request.getParameter(SORT_FIELD);
            if (StrUtil.isNotBlank(sortRule)) {
                if (StrUtil.equalsIgnoreCase(sortRule, DESC)) {
                    return Boolean.FALSE;
                }
            }
        } catch (Exception e) {
            log.warn("Failed to get collation", e);
        }
        return Boolean.TRUE;
    }
}
